home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-06-26 | 21.8 KB | 771 lines | [TEXT/MPS ] |
- #include <Types.h>
- #include <Memory.h>
- #include <QuickDraw.h>
- #include <Fonts.h>
- #include <Packages.h>
- #include <TextUtils.h>
-
- #include <LowMem.h> // LMSetHiliteMode()
-
- #include "TextView.h"
-
-
- #ifndef qSelectionByCharacter
- #define qSelectionByCharacter 1
- #endif
-
- // Private Function Prototypes
-
- static void SetHiliteMode(void)
- {
- LMSetHiliteMode(LMGetHiliteMode() & ~(1 << 7));
- }
-
- static pascal void TextViewActionProc(ControlHandle theControl,short partCode);
-
- ControlActionUPP gTextViewActionUPP = NewControlActionProc(TextViewActionProc);
-
-
- TTextViewHandle NewTextView(WindowPtr theWindow,Rect *viewRect)
- {
- TTextViewHandle theTextView;
- Rect vScrollbarRect,hScrollbarRect;
-
- theTextView = (TTextViewHandle) NewHandle(sizeof(TTextView));
- if (theTextView != nil)
- {
- (*theTextView)->fWindow = theWindow;
- (*theTextView)->fViewRect = *viewRect;
- #ifdef qSelectionByCharacter
- (*theTextView)->fSelectionStart = 0;
- (*theTextView)->fSelectionEnd = 0;
- #else
- (*theTextView)->fSelectionStart = -1;
- #endif
- (*theTextView)->fTopLine = 0;
- (*theTextView)->fLineCount = 0;
- (*theTextView)->fTextLineInfoArrayHandle = (TTextLineInfoHandle) NewHandle(0);
- (*theTextView)->fTextHandle = NewHandle(0);
-
- SetRect(&vScrollbarRect,viewRect->right-kScrollBarThickness,viewRect->top-1,viewRect->right+1,viewRect->bottom-kScrollBarThickness+1);
- SetRect(&hScrollbarRect,viewRect->left-1,viewRect->bottom-kScrollBarThickness,viewRect->right-kScrollBarThickness+1,viewRect->bottom+1);
-
- (*theTextView)->fVerticalScroll = NewControl(theWindow,&vScrollbarRect,"\p",true,0,0,0,scrollBarProc,(unsigned long) theTextView);
- (*theTextView)->fHorizontalScroll = NewControl(theWindow,&hScrollbarRect,"\p",true,0,0,0,scrollBarProc,(unsigned long) theTextView);
-
- SetRect(&(*theTextView)->fTextSizeRect,0,0,0,0);
- }
-
- return(theTextView);
- }
-
-
- void DisposeTextView(TTextViewHandle theTextView)
- {
- DisposeControl((*theTextView)->fVerticalScroll);
- DisposeControl((*theTextView)->fHorizontalScroll);
- DisposeHandle((Handle) (*theTextView)->fTextLineInfoArrayHandle);
- DisposeHandle((*theTextView)->fTextHandle);
- DisposeHandle((Handle) theTextView);
- }
-
-
- void UpdateTextView(TTextViewHandle theTextView,RgnHandle updateRgn)
- {
- char textViewHState,lineInfoHState,textHState;
- short lineIndex;
- char *textStart,*textEnd,*selStart,*selEnd;
- short textLength;
- TTextLineInfoPtr theTextLineInfoArray;
- TTextLineInfoPtr theTextLine;
- FontInfo theFontInfo;
- short lineHeight,verticalPos;
- Rect textRect,lineRect,updateRect,selectRect;
- Boolean selectionExists;
- short horizontalScrollAdjustment;
- RgnHandle oldClip = NewRgn();
- RgnHandle textRgn = NewRgn();
- RgnHandle newClip = NewRgn();
-
- GetClip(oldClip);
-
- horizontalScrollAdjustment = -kHScrollAmount * GetCtlValue((*theTextView)->fHorizontalScroll);
-
- updateRect = (*updateRgn)->rgnBBox; // get bounding rectangle for update region
-
- textRect = (*theTextView)->fViewRect; // clip to prevent drawing over scrollbars & grow area
- textRect.right -= kScrollBarThickness;
- textRect.bottom -= kScrollBarThickness;
-
- RectRgn(textRgn,&textRect);
- SectRgn(textRgn,updateRgn,newClip);
- SetClip(newClip);
-
- EraseRect(&updateRect);
-
- GetFontInfo(&theFontInfo);
- lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
- verticalPos = theFontInfo.ascent;
- SetRect(&lineRect,0,0,textRect.right,lineHeight);
-
- // Lock everything down, because we can move memory from here on out
-
- textViewHState = HGetState((Handle) theTextView);
- lineInfoHState = HGetState((Handle) (*theTextView)->fTextLineInfoArrayHandle);
- textHState = HGetState((Handle) (*theTextView)->fTextHandle);
- HLock((Handle) theTextView);
- HLock((Handle) (*theTextView)->fTextLineInfoArrayHandle);
- HLock((Handle) (*theTextView)->fTextHandle);
-
- theTextLineInfoArray = *((*theTextView)->fTextLineInfoArrayHandle);
- theTextLine = &theTextLineInfoArray[(*theTextView)->fTopLine];
-
- selStart = *((*theTextView)->fTextHandle) + (*theTextView)->fSelectionStart;
- selEnd = *((*theTextView)->fTextHandle) + (*theTextView)->fSelectionEnd;
-
- #ifdef qSelectionByCharacter
- selectionExists = selStart != selEnd;
- #endif
-
- for (lineIndex=(*theTextView)->fTopLine;lineIndex < (*theTextView)->fLineCount;lineIndex++)
- {
- textStart = *((*theTextView)->fTextHandle) + theTextLine->fOffsetFromTextStart;
- textLength = theTextLine->fLineLength;
- textEnd = textStart + textLength -1;
-
- if (lineRect.top > updateRect.bottom) // if we’re off the bottom of the update rectangle, stop drawing
- break;
-
- if (!RectInRgn(&lineRect,updateRgn)) // if we aren’t in the update region, skip drawing
- {
- theTextLine++; // next iteration
- OffsetRect(&lineRect,0,lineHeight);
- verticalPos += lineHeight;
-
- continue;
- }
-
- TextFace(theTextLine->fTextStyle);
- MoveTo(kTextViewMargin + horizontalScrollAdjustment,verticalPos);
- DrawText(textStart,0,textLength);
-
- // Draw selection here if required
-
- #ifdef qSelectionByCharacter
- ////////////////////////////////////////////////////////////////////
- //
- // Useful code for when character by character selection works
- //
- if (selectionExists)
- {
- if ((selStart <= textEnd) && (selEnd >= textStart)) // if (text is within the selection)
- {
- selectRect = lineRect; // first assume text is entirely inside selection
-
- if (selStart > textStart) // start of selection after beginning of line
- {
- selectRect.left = kTextViewMargin + horizontalScrollAdjustment + Char2Pixel(textStart,textLength,0,selStart-textStart,1);
- }
-
- if (selEnd < textEnd) // end of selection is not at or beyond end of line
- {
- selectRect.right = kTextViewMargin + horizontalScrollAdjustment + Char2Pixel(textStart,textLength,0,textEnd-selEnd,1);
- }
-
- SetHiliteMode();
- InvertRect(&selectRect);
- }
- }
- #else
- ////////////////////////////////////////////////////////////////////
- //
- // Chezzy code for doing line-by-line selections
- //
- if ((*theTextView)->fSelectionStart == lineIndex)
- {
- SetHiliteMode();
- InvertRect(&lineRect);
- }
- #endif
-
- theTextLine++; // next iteration
- OffsetRect(&lineRect,0,lineHeight);
- verticalPos += lineHeight;
- }
-
- HSetState((Handle) theTextView,textViewHState);
- HSetState((Handle) (*theTextView)->fTextLineInfoArrayHandle,lineInfoHState);
- HSetState((Handle) (*theTextView)->fTextHandle,textHState);
-
- SetClip(oldClip);
- DisposeRgn(newClip);
- DisposeRgn(textRgn);
- DisposeRgn(oldClip);
- }
-
-
-
- void PrintHeader(Rect *pageRectangle,StringPtr documentName,short pageNum,short baseLine)
- {
- Str255 pageNumString;
- Str255 dateStr,timeStr;
- unsigned long ticks;
-
- TextFace(kPrintHeaderStyle);
-
- // Draw left-justified date and time
-
- GetDateTime(&ticks);
- IUDateString(ticks,shortDate,dateStr);
- IUTimeString(ticks,false,timeStr);
- MoveTo(0,baseLine);
- DrawString(dateStr);
- DrawString("\p ");
- DrawString(timeStr);
-
- // Draw right-justified “Page <number>”
-
- NumToString((long) pageNum,pageNumString);
- MoveTo(pageRectangle->right-StringWidth(pageNumString)-StringWidth("\pPage ")-4,baseLine);
- DrawString("\pPage ");
- DrawString(pageNumString);
-
- // Draw Centered document title
-
- MoveTo((pageRectangle->right + pageRectangle->left - StringWidth(documentName))/2,baseLine);
- DrawString(documentName);
- }
-
-
- void PrintPages(short fontNum,short fontSize,TPPrPort thePrPort,Rect *pageRectangle,StringPtr documentName,TTextViewHandle theTextView)
- {
- char textViewHState,lineInfoHState,textHState;
- char *textStart,*textEnd;
- short textLength;
- TTextLineInfoPtr theTextLineInfoArray;
- TTextLineInfoPtr theTextLine;
- FontInfo theFontInfo;
- short lineHeight,verticalPos;
- short linesPerPage,linesToPrint;
- short lineIndex;
- short pageNum;
-
- TextFont(fontNum);
- TextSize(fontSize);
-
- GetFontInfo(&theFontInfo);
- lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
- linesPerPage = (pageRectangle->bottom - pageRectangle->top - 2*lineHeight)/lineHeight;
-
- // Lock everything down, because we can move memory from here on out
-
- textViewHState = HGetState((Handle) theTextView);
- lineInfoHState = HGetState((Handle) (*theTextView)->fTextLineInfoArrayHandle);
- textHState = HGetState((Handle) (*theTextView)->fTextHandle);
-
- HLock((Handle) theTextView);
- HLock((Handle) (*theTextView)->fTextLineInfoArrayHandle);
- HLock((Handle) (*theTextView)->fTextHandle);
-
- theTextLineInfoArray = *((*theTextView)->fTextLineInfoArrayHandle);
-
- theTextLine = &theTextLineInfoArray[0];
- pageNum = 0;
- lineIndex = 0;
- linesToPrint = (*theTextView)->fLineCount;
-
- while (linesToPrint--)
- {
- if (lineIndex == 0)
- {
- if (pageNum != 0) // close previous page if we’re at a page break
- PrClosePage(thePrPort);
-
- PrOpenPage(thePrPort,(TPRect) nil); // open a new page
- pageNum++;
-
- TextFont(fontNum); // reset the font
- TextSize(fontSize);
-
- verticalPos = pageRectangle->top + theFontInfo.ascent;
- PrintHeader(pageRectangle,documentName,pageNum,verticalPos);
- verticalPos += 2*lineHeight;
- }
-
- textStart = *((*theTextView)->fTextHandle) + theTextLine->fOffsetFromTextStart;
- textLength = theTextLine->fLineLength;
- textEnd = textStart + textLength -1;
-
- TextFace(theTextLine->fTextStyle);
- MoveTo(kTextViewMargin,verticalPos);
- DrawText(textStart,0,textLength);
-
- theTextLine++; // next iteration
- verticalPos += lineHeight;
- lineIndex = (lineIndex + 1) % linesPerPage;
- }
-
- if (pageNum != 0)
- PrClosePage(thePrPort);
-
- HSetState((Handle) theTextView,textViewHState);
- HSetState((Handle) (*theTextView)->fTextLineInfoArrayHandle,lineInfoHState);
- HSetState((Handle) (*theTextView)->fTextHandle,textHState);
- }
-
-
-
-
- void PrintTextView(THPrint printRecord,StringPtr documentName,TTextViewHandle theTextView)
- {
- TPPrPort aPrPort;
- short fontNum,fontSize;
-
- fontNum = qd.thePort->txFont;
- fontSize = qd.thePort->txSize;
-
- aPrPort= PrOpenDoc(printRecord,(TPPrPort) nil,(Ptr) nil);
- if (PrError() == noErr)
- PrintPages(fontNum,fontSize,aPrPort,&(*printRecord)->prInfo.rPage,documentName,theTextView);
- PrCloseDoc(aPrPort);
- }
-
-
- void ActivateTextView(TTextViewHandle theTextView,Boolean activating)
- {
- (*theTextView)->fActive = activating;
-
- if (activating)
- {
- HiliteControl((*theTextView)->fVerticalScroll,0);
- HiliteControl((*theTextView)->fHorizontalScroll,0);
- }
- else
- {
- HiliteControl((*theTextView)->fVerticalScroll,254);
- HiliteControl((*theTextView)->fHorizontalScroll,254);
- }
- }
-
-
- Boolean ClickTextView(TTextViewHandle theTextView,Point where,long *lineHit,Boolean isDoubleClick)
- {
- GrafPtr oldPort;
- ControlHandle theControl;
- short partCode;
- short oldValue,newValue;
- Rect textRect;
- FontInfo theFontInfo;
- short lineHeight;
-
- GetPort(&oldPort);
- SetPort((*theTextView)->fWindow);
- GetFontInfo(&theFontInfo);
- lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
- SetPort(oldPort);
-
- textRect = (*theTextView)->fViewRect;
- textRect.right -= 15;
- textRect.bottom -= 15;
-
- if (PtInRect(where,&textRect))
- {
- *lineHit = (where.v - textRect.top) / lineHeight + (*theTextView)->fTopLine;
- if (*lineHit < (*theTextView)->fLineCount)
- {
- SelectLine(theTextView,*lineHit);
- return(isDoubleClick);
- }
- else
- return(false);
- }
- else if (PtInRect(where,&(*theTextView)->fViewRect))
- {
- partCode = FindControl(where,(*theTextView)->fWindow,&theControl);
- if (partCode != 0)
- {
- if (partCode == inThumb)
- {
- oldValue = GetCtlValue(theControl);
- TrackControl(theControl,where,(ControlActionUPP) nil);
- newValue = GetCtlValue(theControl);
- if (newValue != oldValue)
- {
- if (theControl == (*theTextView)->fVerticalScroll)
- (*theTextView)->fTopLine = newValue;
-
- InvalRect(&(*theTextView)->fViewRect);
- }
- }
- else
- TrackControl(theControl,where,gTextViewActionUPP);
- }
- }
- return(false);
- }
-
-
- void SelectLine(TTextViewHandle theTextView,long lineHit)
- {
- GrafPtr oldPort;
- TTextLineInfo ;
- FontInfo theFontInfo;
- short lineHeight;
- Rect newRect,oldRect;
-
- GetPort(&oldPort);
- SetPort((*theTextView)->fWindow);
- GetFontInfo(&theFontInfo);
- lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
-
- SetRect(&newRect,0,0,(*theTextView)->fViewRect.right-15,lineHeight);
- oldRect = newRect;
- OffsetRect(&oldRect,0,((*theTextView)->fSelectionStart-(*theTextView)->fTopLine)*lineHeight);
- OffsetRect(&newRect,0,(lineHit-(*theTextView)->fTopLine)*lineHeight);
-
- SetHiliteMode(); // get rid of old selection
- InvertRect(&oldRect);
-
- SetHiliteMode(); // select new line
- InvertRect(&newRect);
-
- (*theTextView)->fSelectionStart = lineHit;
-
- SetPort(oldPort);
- }
-
-
- unsigned long GetTextLineRefCon(TTextViewHandle theTextView,long theLine)
- {
- TTextLineInfo theTextLine;
- TTextLineInfo *theTextLineInfoArray;
-
- theTextLineInfoArray = *((*theTextView)->fTextLineInfoArrayHandle);
- theTextLine = theTextLineInfoArray[theLine];
-
- return(theTextLine.fRefCon);
- }
-
-
- pascal void TextViewActionProc(ControlHandle theControl,short partCode)
- {
- TTextViewHandle theTextView;
- long topLine,lastTopLine;
- short lineHeight;
- Rect textRect;
- RgnHandle updateRgn = NewRgn();
- FontInfo theFontInfo;
- short pageDistanceInPixels;
- short pageDistanceInWholeLines;
- short scrollDistance;
- short hScroll,hScrollMax;
-
- GetFontInfo(&theFontInfo);
- lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
-
- // a handle to the TTextViewHandle is stored in the refCon
-
- theTextView = (TTextViewHandle) GetCRefCon(theControl);
-
- topLine = (*theTextView)->fTopLine;
- lastTopLine = GetCtlMax(theControl);
-
- textRect = (*theTextView)->fViewRect;
- textRect.right -= kScrollBarThickness;
- textRect.bottom -= kScrollBarThickness;
-
- pageDistanceInWholeLines = (textRect.bottom - textRect.top) / lineHeight;
- pageDistanceInPixels = (pageDistanceInWholeLines * lineHeight);
- scrollDistance = 0;
-
-
- if (theControl == (*theTextView)->fVerticalScroll)
- {
- switch (partCode)
- {
- case inUpButton:
- topLine--;
- if (topLine >= 0)
- scrollDistance = lineHeight;
- break;
-
- case inDownButton:
- topLine++;
- if (topLine <= lastTopLine)
- scrollDistance = -lineHeight;
- break;
-
- case inPageUp:
- topLine -= pageDistanceInWholeLines;
- scrollDistance = pageDistanceInPixels;
-
- if (topLine < 0)
- {
- scrollDistance += (topLine * lineHeight); // subtract off error in scrolldistance
- topLine = 0;
- }
- break;
-
- case inPageDown:
- topLine += pageDistanceInWholeLines;
- scrollDistance = -pageDistanceInPixels;
-
- if (topLine > lastTopLine)
- {
- scrollDistance += ((lastTopLine-topLine) * lineHeight); // subtract off error in scrolldistance
- topLine = lastTopLine;
- }
- break;
- }
-
- if (scrollDistance != 0)
- {
- (*theTextView)->fTopLine = topLine;
- SetCtlValue(theControl,topLine);
- ScrollRect(&textRect,0,scrollDistance,updateRgn);
- UpdateTextView(theTextView,updateRgn);
- }
- }
- else if (theControl == (*theTextView)->fHorizontalScroll)
- {
- hScroll = GetCtlValue(theControl);
- hScrollMax = GetCtlMax(theControl);
- switch (partCode)
- {
- case inUpButton:
- if (hScroll > 0)
- {
- hScroll--;
- scrollDistance = -1;
- }
- break;
-
- case inDownButton:
- if (hScroll < hScrollMax)
- {
- hScroll++;
- scrollDistance = 1;
- }
- break;
-
- case inPageUp:
- hScroll -= 5;
- scrollDistance = -5;
- if (hScroll < 0)
- {
- scrollDistance += -hScroll; // add undershoot
- hScroll = 0;
- }
- break;
-
- case inPageDown:
- hScroll += 5;
- scrollDistance = 5;
- if (hScroll > hScrollMax)
- {
- scrollDistance -= (hScroll-hScrollMax); // subtract overshoot
- hScroll = hScrollMax;
- }
- break;
-
- default:
- break;
- }
-
- // Update hScroll & update the report window
-
- SetCtlValue(theControl,hScroll);
- ScrollRect(&textRect,-kHScrollAmount*scrollDistance,0,updateRgn);
- UpdateTextView(theTextView,updateRgn);
- }
-
- ResizeTextView(theTextView,&(*theTextView)->fViewRect);
- DisposeRgn(updateRgn);
- }
-
-
- void ScrollTextView(TTextViewHandle theTextView,unsigned long newTopLine)
- {
- GrafPtr oldPort;
- Rect textRect;
-
- if (newTopLine > (*theTextView)->fLineCount-1)
- newTopLine = (*theTextView)->fLineCount;
-
- textRect = (*theTextView)->fViewRect;
- textRect.right -= kScrollBarThickness;
-
- GetPort(&oldPort);
- SetPort((*theTextView)->fWindow);
- SetCtlValue((*theTextView)->fVerticalScroll,newTopLine);
- InvalRect(&textRect);
- SetPort(oldPort);
- }
-
-
- void SetTextViewSelection(TTextViewHandle theTextView,unsigned long selStart,unsigned long selEnd)
- {
- unsigned long temp;
-
- if (selStart > selEnd)
- {
- temp = selStart;
- selStart = selEnd;
- selEnd = temp;
- }
-
- (*theTextView)->fSelectionStart = selStart;
- (*theTextView)->fSelectionEnd = selEnd;
- }
-
-
- void GetTextViewSelection(TTextViewHandle theTextView,unsigned long *selStart,unsigned long *selEnd)
- {
- *selStart = (*theTextView)->fSelectionStart;
- *selEnd = (*theTextView)->fSelectionEnd;
- }
-
-
- void ResizeTextView(TTextViewHandle theTextView,Rect *newSize)
- {
- GrafPtr oldPort;
- Rect viewRect,vScrollbarRect,hScrollbarRect;
- short lineHeight,newHeightInLines,newVScrollMax;
- short maxTextWidth,windowWidth,oldHScroll,newHScrollMax;
- FontInfo theFontInfo;
-
- GetPort(&oldPort);
- SetPort((*theTextView)->fWindow);
-
- GetFontInfo(&theFontInfo);
- lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
-
- viewRect = (*theTextView)->fViewRect;
-
- if (!EqualRect(&viewRect,newSize))
- {
- // If we really changed size, then move the scrollbars around!
-
- // Invalidate vertical scrollbar rectangle and horizontal scrollbar rectangle and grow box
-
- SetRect(&vScrollbarRect,viewRect.right-kScrollBarThickness,viewRect.top-1,viewRect.right+1,viewRect.bottom-kScrollBarThickness+1);
- SetRect(&hScrollbarRect,viewRect.left-1,viewRect.bottom-kScrollBarThickness,viewRect.right-1,viewRect.bottom+1);
- InvalRect(&vScrollbarRect);
- InvalRect(&hScrollbarRect);
-
- // move the scrollbars to new locations and redraw them
-
- HideControl((*theTextView)->fVerticalScroll);
- HideControl((*theTextView)->fHorizontalScroll);
- MoveControl((*theTextView)->fVerticalScroll,newSize->right-kScrollBarThickness,newSize->top-1);
- SizeControl((*theTextView)->fVerticalScroll,16,newSize->bottom-newSize->top-kScrollBarThickness+2);
- MoveControl((*theTextView)->fHorizontalScroll,newSize->left-1,newSize->bottom-kScrollBarThickness);
- SizeControl((*theTextView)->fHorizontalScroll,newSize->right-newSize->left-kScrollBarThickness+2,16);
- DrawControls((*theTextView)->fWindow);
- ShowControl((*theTextView)->fVerticalScroll);
- ShowControl((*theTextView)->fHorizontalScroll);
-
- // Validate the new scrollbar rectangles (so we don’t double draw)
-
- SetRect(&vScrollbarRect,newSize->right-kScrollBarThickness,newSize->top-1,newSize->right+1,newSize->bottom-kScrollBarThickness+1);
- SetRect(&hScrollbarRect,newSize->left-1,newSize->bottom-kScrollBarThickness,newSize->right-kScrollBarThickness+1,newSize->bottom+1);
- ValidRect(&vScrollbarRect);
- ValidRect(&hScrollbarRect);
-
- /* Invalidate the grow box too */
-
- viewRect = *newSize;
- viewRect.left = viewRect.right-15;
- viewRect.top = viewRect.bottom - 15;
- InvalRect(&viewRect);
-
- // Update the TextView Record
-
- (*theTextView)->fViewRect = *newSize;
- }
-
- // ---> Regardless of whether or not this is a new size, recalc the scrollbar max values
-
- // Update maximum value for vertical scroll
-
- newHeightInLines = (newSize->bottom-newSize->top-kScrollBarThickness-kTextViewMargin) / lineHeight;
- newVScrollMax = (*theTextView)->fLineCount - newHeightInLines;
- if (newVScrollMax < 0)
- newVScrollMax = (*theTextView)->fTopLine;
- SetCtlMax((*theTextView)->fVerticalScroll,newVScrollMax);
-
-
- // Update maximum value for horizontal scroll
-
- maxTextWidth = (*theTextView)->fTextSizeRect.right - (*theTextView)->fTextSizeRect.left;
- windowWidth = newSize->right-newSize->left-kTextViewMargin-kScrollBarThickness-kHScrollAmount;
-
- oldHScroll = GetCtlValue((*theTextView)->fHorizontalScroll);
-
- newHScrollMax = (maxTextWidth - windowWidth) / kHScrollAmount;
- if (newHScrollMax < oldHScroll)
- newHScrollMax = oldHScroll;
- SetCtlMax((*theTextView)->fHorizontalScroll,newHScrollMax);
- SetPort(oldPort);
- }
-
-
- void AddStringToTextView(TTextViewHandle theTextView,Str255 theTextToAdd,Style theTextStyle,long refCon)
- {
- GrafPtr oldPort;
- Size textHandleSize,lineInfoArrayHandleSize;
- TTextLineInfoPtr theTextLine;
- long stringLength;
- char *eolPtr;
- FontInfo theFontInfo;
- short lineHeight,lineWidth;
-
- GetPort(&oldPort);
- SetPort((*theTextView)->fWindow);
-
- GetFontInfo(&theFontInfo);
- lineHeight = theFontInfo.ascent + theFontInfo.descent + theFontInfo.leading;
-
- stringLength = theTextToAdd[0];
-
- textHandleSize = GetHandleSize((*theTextView)->fTextHandle);
- SetHandleSize((*theTextView)->fTextHandle,textHandleSize + stringLength+1);
-
- if (MemError() != noErr) // unable to bump text handle up, so punt
- return;
-
- lineInfoArrayHandleSize = GetHandleSize((Handle) (*theTextView)->fTextLineInfoArrayHandle);
- SetHandleSize((Handle) (*theTextView)->fTextLineInfoArrayHandle, lineInfoArrayHandleSize + sizeof(TTextLineInfo));
-
- if (MemError() != noErr) // unable to alloc an additional lineinfo record, punt!
- {
- SetHandleSize((*theTextView)->fTextHandle,textHandleSize);
- return;
- }
-
- theTextLine = &(*(*theTextView)->fTextLineInfoArrayHandle)[(*theTextView)->fLineCount];
- theTextLine->fOffsetFromTextStart = textHandleSize;
- theTextLine->fLineLength = stringLength;
- theTextLine->fRefCon = refCon;
- theTextLine->fTextStyle = theTextStyle;
-
- (*theTextView)->fLineCount++; // bump line count
-
- TextFace(theTextStyle);
- lineWidth = StringWidth(theTextToAdd);
-
- if (lineWidth > (*theTextView)->fTextSizeRect.right-(*theTextView)->fTextSizeRect.left)
- (*theTextView)->fTextSizeRect.right = (*theTextView)->fTextSizeRect.left + lineWidth;
-
- (*theTextView)->fTextSizeRect.bottom += lineHeight; // bump textSizeRectangle
-
- // copy string to text buffer
-
- BlockMove(&theTextToAdd[1],*((*theTextView)->fTextHandle)+textHandleSize,stringLength);
-
- eolPtr = *((*theTextView)->fTextHandle)+textHandleSize+stringLength;
- *eolPtr = 13;
-
- SetPort(oldPort);
- }
-